/*
 * Copyright 2011 Noctarius (Christoph Engelbert)
 * Part of www.sourceprojects.org 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may 
 * not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package org.sourceprojects.cappadocia.builder;

import java.lang.annotation.Annotation;

class ClassAttributeConstraintSelectorImpl extends AbstractSelectorElement
		implements ClassAttributeConstraintSelector {

	private final Class<? extends Annotation> annotation;
	private final LikeExpression likeExpression;
	private final String interfaceClassname;
	private final String superclassClassname;

	private boolean and = false;
	private boolean or = false;

	ClassAttributeConstraintSelectorImpl(final AbstractSelectorElement parent,
			final Class<? extends Annotation> annotation) {

		super(parent);

		this.annotation = annotation;
		this.likeExpression = null;
		this.interfaceClassname = null;
		this.superclassClassname = null;
	}

	ClassAttributeConstraintSelectorImpl(final AbstractSelectorElement parent,
			final LikeExpression likeExpression) {

		super(parent);

		this.annotation = null;
		this.likeExpression = likeExpression;
		this.interfaceClassname = null;
		this.superclassClassname = null;
	}

	ClassAttributeConstraintSelectorImpl(final AbstractSelectorElement parent,
			final String classname, final boolean superclass) {

		super(parent);

		this.annotation = null;
		this.likeExpression = null;
		this.interfaceClassname = !superclass ? classname : null;
		this.superclassClassname = superclass ? classname : null;
	}

	@Override
	public ClassAttributeSelector and() {
		and = true;
		return new ClassAttributeSelectorImpl(this);
	}

	@Override
	public ClassAttributeSelector or() {
		or = true;
		return new ClassAttributeSelectorImpl(this);
	}

	@Override
	public String toQuery() {
		final StringBuilder builder = new StringBuilder();

		if (getParent() != null) {
			builder.append(" ").append(getParent().toQuery());
		}

		if (annotation != null) {
			builder.append(" annotatedWith('")
					.append(annotation.getCanonicalName()).append("')");

		} else if (likeExpression != null) {

		} else if (interfaceClassname != null) {
			builder.append(" implementationOf('").append(interfaceClassname)
					.append("')");

		} else if (superclassClassname != null) {
			builder.append(" subclassOf('").append(superclassClassname)
					.append("')");

		}

		if (and) {
			builder.append(" AND ");

		} else if (or) {
			builder.append(" OR ");
		}

		return builder.toString();
	}

}
